#include "SoundManager.h"
#include "Globals.h"

SoundManager* SoundManager::_instance = NULL;
bool SoundManager::isBGM_On = true;
bool SoundManager::isSoundEffect_On = true;
SoundManager* SoundManager::GetInst()
{
	if(SoundManager::_instance == NULL)
	{
		_instance = new SoundManager();
	}
	return _instance;
}

SoundManager::SoundManager()
{
	_audio = new CAudio();
	_audio->Init_DirectSound(GL_graphic->GethWindow());

	bgmMenu = _audio->LoadSound("Resource\\Sounds\\Menu.wav");
	bgmGame1 = _audio->LoadSound("Resource\\Sounds\\Map1.wav");
	bgmGame2 = _audio->LoadSound("Resource\\Sounds\\Map2.wav");
	jumpSound = _audio->LoadSound("Resource\\Sounds\\Jump.wav");
	lifeUpSound = _audio->LoadSound("Resource\\Sounds\\LifeUp.wav");
	growUpSound = _audio->LoadSound("Resource\\Sounds\\MarioGrow.wav");
	dieSound = _audio->LoadSound("Resource\\Sounds\\Death.wav");
	shootSound = _audio->LoadSound("Resource\\Sounds\\Shotted.wav");
	
	breakBrickSound = _audio->LoadSound("Resource\\Sounds\\BrickBreaked.wav");
	itemUpSound = _audio->LoadSound("Resource\\Sounds\\ItemUp.wav");
	coinSound = _audio->LoadSound("Resource\\Sounds\\Coin.wav");
	kickUp = _audio->LoadSound("Resource\\Sounds\\MarioKick.wav");
	fungiDie = _audio->LoadSound("Resource\\Sounds\\Mushroom Die.wav");
	turtleHit = _audio->LoadSound("Resource\\Sounds\\TurTleHead.wav");

	win = _audio->LoadSound("Resource\\Sounds\\stage_clear.wav");
	lose = _audio->LoadSound("Resource\\Sounds\\lose.wav");
	click = _audio->LoadSound("Resource\\Sounds\\Click.wav");
}

SoundManager::~SoundManager()
{

}

void SoundManager::PlayBGSound(EBGSound id)
{
	
	switch(id)
	{
	case EBGSound::EMenuSound:
		
		_listBgm.push_back(bgmMenu);
		if(isBGM_On==true)
		{
			bgmMenu->Reset();
			_audio->LoopSound(bgmMenu);
		}
		break;
	case EBGSound::EMainGameSound1:
		_listBgm.push_back(bgmGame1);
		if(isBGM_On==true)
		{
			bgmGame1->Reset();
			_audio->LoopSound(bgmGame1);
		}
		
		break;
	case EBGSound::EMainGameSound2:
		_listBgm.push_back(bgmGame2);
		if(isBGM_On==true)
		{
			bgmGame2->Reset();
			_audio->LoopSound(bgmGame2);
		}
		
		break;
	}
}

void SoundManager::PlaySoundEffect(ESoundEffect id)
{
	if(isSoundEffect_On==false)
		return;
	switch(id)
	{
	case ESoundEffect::ES_MarioJump:
		jumpSound->Play();
		break;
	case ESoundEffect::ES_MarioDie:
		dieSound->Play();
		break;
	case ESoundEffect::ES_MarioLifeUp:
		lifeUpSound->Play();
		break;
	case ESoundEffect::ES_MarioGrowUp:
		growUpSound->Play();
		break;
	case ESoundEffect::ES_Shoot:
		shootSound->Play();
		break;
	case ESoundEffect::ES_BreakBrick:
		breakBrickSound->Play();
		break;
	case ESoundEffect::ES_Coin:
		coinSound->Play();
		break;
	case ESoundEffect::ES_ItemUp:
		itemUpSound->Play();
		break;
	case ESoundEffect::ES_KickUp:
		kickUp->Play();
		break;
	case ESoundEffect::ES_FungiDie:
		fungiDie->Play();
		break;
	case ESoundEffect::ES_TurtleHit:
		turtleHit->Play();
		break;
	case ESoundEffect::ES_Win:
		win->Play();
		break;
	case ESoundEffect::ES_Lose:
		lose->Play();
		break;
	case ESoundEffect::ES_Click:
		click->Play();
		break;
	}
}

void SoundManager::StopAllBGSound()
{
	for (int i = 0; i<_listBgm.size(); i++)
	{
		_listBgm[i]->Stop();
	}
}

void SoundManager::RemoveAllBGM()
{
	StopAllBGSound();
	_listBgm.clear();
}
void SoundManager::ResumeBGM()
{
	for (int i = 0; i<_listBgm.size(); i++)
	{
		_audio->PlaySoundA(_listBgm[i]);
	}
}